unit dbcTypes;
// Database Comparer VCL v.3.0
// Copyright (C) 1998-2006, by Boris Loboda
// www.CleverComponents.com
{$I Comparer.INC}

interface

Uses Classes, SysUtils{$ifdef Delphi2009}, Windows{$endif};

type
  TDatabaseType = (dbAbstract,
                   dbInterBase,
                   dbSybaseASA,
                   dbMySQL,
                   dbOracle,
                   dbMSSQL,
                   dbPostgreSQL,
                   dbDB2,
                   dbSybaseASE,
                   dbPervasiveSQL,
                   dbMSAccess,
                   dbParadox,
                   dbDBASE,
                   dbCustomOLEDB);

  TDatabaseSetType = set of TDatabaseType;

  TconnectionType = (cnAbstract,
                     cnIBX,
                     cnFIB,
                     cnIBO,
                     cnADO,
                     cnZEOS,
                     cnBDE,
                     cnDBX,
                     cnAnyDAC,
                     cnUIB,
                     cnODAC,
                     cnSAVCL,
                     cnMyDAC,
                     cnPostgresDAC,
                     cnIBDAC);

  TConnectionSetType = set of TConnectionType;

  TDBCProtocol = (ptTCP, ptSPX, ptNamedPipe, ptLocal);

type
  TCmpObjType = (coDomain, coFunction,
                 coGenerator, coException,
                 coTable, coView, coProcedure, coTrigger, coIndex,
                 coRole, coGrant,
                 coRoleGrant, coUserGrant, coObjGrant,        // role/user/object grant
                 coPrimaryKey, coForeignKey, coUnique,
                 coCheck,
                 coRule, coDefault, coCtrDefault,             // MS SQL
                 coPackage,                                   // Oracle
                 coDescription);

  TCmpObjSetType = set of TCmpObjType;

const
  DefCmpObjSet = [coDomain..CoDescription];

type
  TCompareRoleType = (crUnknownRole, crMasterRole, crTargetRole);
  TExtractFromType = (efUnknown, efDatabase, efScript);

  // statement (script)
  TStatType = (stConnect,     // CONNECT... / USE...
               stReconnect,   // RECONNECT
               stCreateDb,    // CREATE DATABASE
               stSet,         // SET ...
               stSetBlobFile, // SET BLOBFILE <filename>
               st_Commit,     // COMMIT [WORK]
               stStatement,   // typical statement
               stComment,     // /* comment */
               stProcedure,   // CREATE / ALTER PROCEDURE
               stTrigger,     // CREATE / ALTER TRIGGER
               stSetNames,    // SET NAMES <charset>
               stCharSet,     // CHARACTER SET <charset>
               stSqlDialect,  // SET SQL DIALECT <n>
               stDescription, // object-description (COMMENT-statement & etc.)
               stSelect,      // SELECT ...
               stInsert,      // INSERT ...
               stUpdate,      // UPDATE ...
               stDelete,      // DELETE ...
               stExecuteBlock,// EXECUTE BLOCK
               stPackage);    // PACKAGE (Oracle)
const
  StatTypeStr : Array[stConnect..stPackage] of string =
               ('Connect',
                'ReConnect',
                'CreateDb',
                'Set',
                'BlobFile',
                'Commit',
                'Statement',
                'Comment',
                'Procedure',
                'Trigger',
                'SetNames',
                'CharSet',
                'SqlDialect',
                'Description',
                'Select',
                'Insert',
                'Update',
                'Delete',
                'ExecuteBlock',
                'Package');

type
  TDbObject = (dboUnknown, dboTable, dboProcedure, dboTrigger, dboFunction,
               dboException, dboIndex, dboDomain, dboView,
               dboGenerator, dboEvent,
               dboField, dboParam, dboDatabase,
               dboConstraint,
               dboPKey, dboFKey, dboUnique, dboCheck, dboReferences,
               dboCtrDefault,          // MS SQL
               dboRole, dboGrant,
               dboRule, dboDefault,    // MS SQL
               dboPackage,             // Oracle
               dboPublication,         // Sybase
               dboSchema,
               dboOperator,            // PostgreSQL
               dboOperatorClass,       // PostgreSQL
               dboConversion,
               dboUser, dboUserGroup,
               dboValidation, dboExprIndex,
               dboCount, dboComputed, dboDepend,
               dboPrkList, dboFrkList, dboUnqList,
               dboChkList, dboDefList, dboRefList,
               dboIdxList, dboExcList, dboRolList,
               dboGntList, dboCdfList, dboPrcList,
               dboTrgList, dboFncList, dboRulList,
               dboGnrList, dboPkgList, dboDmnList,
               dboViwList, dboFldList, dboTblLIst,
               dboPblList, dboOprList, dboOpcList,
               dboCnvList, dboEvtList, dboUseInList,
               dboUseOfList
               );

  TDbObjectSet = Set of TDBObject;

  // statement operation
  TDbOperation = (odbUnknown, odbCreate, odbAlter, odbDrop,
                  odbEmpty, odbRestore,
                  odbEmptyCreate, odbEmptyDrop, odbSet,
                  odbCommit, odbComment, odbDescription,
                  odbInsert, odbUpdate, odbDelete);

  // SQL Anywhere
  TParamInOutType = (ioUnknown, ioIn, ioOut, ioInout, ioVariadic);
  TTblOnCommit = (tocUnknown, tocDelete, tocPreserve);

  TGrantPrivilege = (gntAll, gntInsert, gntUpdate, gntDelete, gntSelect,
                     gntExecute, gntReferences,
                     // MS SQL
                     gntConnect,
                     // SQL Anywhere
                     gntAlter,
                     // Oracle
                     gntCreateDatabaseLink, gntCreateSequence, gntCreateSession,
                     gntCreateTrigger, gntCreateProcedure, gntCreateType,
                     gntCreateSynonym, gntCreatePublicSynonym, gntDropSynonym,
                     gntDropPublicSynonym, gntCreateTable,
                     gntCreateView, gntCreateIndexType,
                     gntCreateRule, gntCreateDefault,
                     gntCreateFunction, gntCreateDatabase,
                     gntBackupDatabase, gntBackupLog,
                     gntDumpDatabase, gntDumpTransaction,
                     gntViewAnyDatabase, gntTruncateTable,
                     gntUpdateStatistics, gntDeleteStatistics,
                     gntUnlimitedTableSpace, gntDropTable,
                     gntDropView, gntDropTrigger, gntDropProcedure,
                     gntDropSequence, gntDropType, gntDropIndexType,
                     gntForceAnyTransaction);

  TGrantPrivilegeSet = Set of TGrantPrivilege;

  // InterBase - update description
  TUpdateDescriptionType = (udSysTables, // update system tables
                           udDescribe,   // DESCRIBE-statement (IBExpert)
                           udComment);   // COMMENT-statement (FB2)

  TOracleDataType = (odUnknown, odSimple, odObject, odVArray, odAsTable, odIsTable);
  TMySQLProcOptions = ( mpLanguageSql,
                        mpDeterministic,
                        mpNotDeterministic,
                        mpContainsSql,
                        mpNoSql,
                        mpReadsSqlData,
                        mpModifiesSqlData,
                        mpSqlSecurityDefiner,
                        mpSqlSecurityInvoker);
  TMySqlProcOptionsSet = set of TMySqlProcOptions;

  // Oracle table space
  TTableSpace = class
  public
    PctFree, PctUsed, IniTrans, MaxTrans: Integer;
    Storage: String;
    function Def(): String;
    procedure Clear;
    function IsEmpty(): Boolean;
    procedure AssignDiffs(ATblSpace: TTableSpace);
  end;

  // Oracle type directives
  TTypeDirective = class
    NotInstantiable, NotFinal,
    Final, Instantiable, Overriding: Boolean;
    function Def(): String;
  end;

  TIndexOptions = class
  public
    PadIndex: Boolean;
    FillFactor: Integer;
    IgnoreDupKey: Boolean;
    StatisticsNorecompute: Boolean;
    AllowRowLocks: Boolean;
    AllowPageLocks: Boolean;
    SortInTempDb: Boolean;
    DropExisting: Boolean;
    Online: Boolean;
    MaxDop: Integer;
    Using: String;
    procedure Clear;
    function Def(): String;
  end;

type
  TASASQLDialect = (asaSybaseSQL, asaTransactSQL);
  
  TIdentQuoteType = (iqAutoQuote, iqNoneQuote, iqSingleQuote, iqDoubleQuote, iqBrackQuote);

type
  TLogProcedure = Procedure(aText: String);

  TProgressUpdateEvent = procedure(Sender: TObject; Value, MaxValue: integer) of Object;
  TProgressUpdateExEvent = procedure(Sender: TObject; Detailed: Boolean; aText: String; Value, MaxValue: integer) of Object;
  TLogNextLineEvent = procedure(Sender: TObject; LogText: string) of Object;
  TErrorMessageEvent = procedure(Sender: TObject; ErrText: String) of Object;
  // load script from file FileName to aScript.
  TLoadScriptEvent = procedure(Sender: TObject;
                               FileName: String;
                               aScript: TStrings;
                               var Loaded: Boolean) of Object;
  TAddStatementEvent = procedure(Sender: TObject;
                                 aText: String;
                                 aTyp: TStatType;
                                 aObjName: String;
                                 aObjType: TDbObject;
                                 aOperation: TDbOperation;
                                 aOwnName: String;
                                 var Statement: TObject) of Object; 

const
  idxNoSpec  = 0;
  idxAsc     = 1;
  idxDesc    = 2;

(*
const
  obj_Unknown = 150;

  obj_dmn  = 100;    // DOMAIN
  obj_tbl  = 101;    // TABLE
  obj_fld  = 102;    // Field (table-, view-field, procedure parameter)
  obj_viw  = 103;    // VIEW
  obj_idx  = 104;    // INDEX
  obj_prc  = 105;    // PROCEDURE
  obj_trg  = 106;    // TRIGGER
  obj_gnr  = 107;    // GENERATOR - IB
  obj_exc  = 108;    // EXCEPTION
  obj_fnc  = 109;    // External Function (UDF) - IB
  obj_rol  = 110;    // ROLE
  obj_gnt  = 111;    // GRANT
  obj_depend = 112;  // Dependencies
  obj_prk    = 113;  // PRIMARY KEY
  obj_frk    = 114;  // FOREIGN KEY
  obj_unq    = 115;  // UNIQUE constraint
  obj_chk    = 116;  // CHECK constraint
  obj_rul    = 117;  // RULE (MS SQL)
  obj_def    = 118;  // DEFAULT (MS SQL)
  obj_cdf    = 119;  // DEFAULT constraint (MS SQL)
  obj_pkg    = 120;  // PACKAGE (Oracle)
  obj_ctr    = 121;  // CONSTRAINT
  obj_ref    = 122;  // REFERENCES constraint
  obj_pbl    = 123;  // PUBLICATION (Sybase)
  obj_sch    = 124;  // SCHEMA
  obj_opr    = 125;  // OPERATOR (PostgreSQL)
  obj_opc    = 126;  // OPERATOR CLASS (PostgreSQL)
  obj_cnv    = 127;  // CONVERSION (PostgreSQL)
  obj_evt    = 128;  // EVENT (Sybase ASA)

  obj_DataBase = 250;
*)

type
  TDbObjSet = set of TDbObject;

  TSymbol = Byte;

  TSymSet = set of TSymbol;

const
  CrLf               = #$D#$A; // next line

function GenScriptTerm(dbType: TDatabaseType): String;

function ScriptQuotedStr(aText: String; aDbType: TDatabaseType): String;

function ValidateFileName(fn: String): String;

function SchObjName(SchName, ObjName: String): String;

function TryStrToInt(const S: string; out Value: Integer): Boolean;

var
  DebugLog: Boolean;

{$ifndef Delphi2009}
type
  TCharSet = Set of Char;

function CharInSet(ch: char; cset: TCharSet): Boolean;
{$endif}

{$ifdef Delphi2009}
type
  TdbcEncode = (encDefault, encANSI, encUnicode, encUTF8, encUTF7);

  TdbcEncoding = class(TEncoding)
  class var
    FAnsiEncoding: TEncoding;
    fEncode: TdbcEncode;
  public
    class function dbcGetBytes(aValue: String; aEncode: TdbcEncode = encDefault): TBytes;
    class property Encode: TdbcEncode read fEncode write fEncode;
    class function dbcGetEncoding(aEncode: TdbcEncode = encDefault): TEncoding;
    class procedure FreeEncodings;
  end;
{$endif}

implementation

//The full source code is available on purchasing the product

end.
